//
//  HOPVisitorController.swift
//  WB01
//
//  Created by HOP on 15/12/15.
//  Copyright © 2015年 HOP. All rights reserved.
//

import UIKit

// 1.懒加载所有控件
// 2.添加控件
// 3.设置位置

// 按钮点击协议
// 需要继承NSObjectProtocol
protocol HOPVisitorViewDelegate: NSObjectProtocol {
    // 点击注册
    func visitouViewRegister()
    // 点击登录
    func visitorViewLogin()
}


class HOPVisitorController: UIView  {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 准备UI
    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
    // Autoresizing影响AutoLayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        // 设置位置
        // 使用AutoLayout来布局
        
        // 约束转轮CenterX和父控件的CenterX重合
        // item: 需要约束的view
        // attribute: 要约束的属性 CenterX
        
        // 将约束对象添加到父控件self上面去
    
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 约束转轮CenterY和父控件的CenterY重合
        // 将约束添加到父控件self上面
            self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -49))
        
        // 房子参照转轮
        // CenterX和转轮的CenterX重合
        // 约束统一添加到父控件上面
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 房子CenterY和转轮的CenterY重合
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 消息label CenterX和转轮CenterX重合
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 消息label的顶部,距离转轮的底部16
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        消息宽度
          self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        // 注册按钮
        // 宽100
        // 不参照任何对象: toItem 填nil attribute: 必须填: NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度35
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 注册按钮顶部距离消息label 底部16
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 注册按钮左边和消息label左边重合
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 登陆按钮
        // 宽100
        // 不参照任何对象: toItem 填nil attribute: 必须填: NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    // 高度35
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 35))
        // 登陆按钮右边和消息label右边重合
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 登陆按钮顶部距离消息label 底部16
        self.addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 遮盖的宽
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
//        遮盖的顶部
         self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        遮盖的底部
         self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    // MARK: - 懒加载
    /// 转轮
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //添加小房子
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    添加文字
    private lazy var messageLabel: UILabel = {
    let label = UILabel()
        label.text = "关注一些人,看看这里有什么惊喜!"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
      return label
    }()
    private lazy var registerButton: UIButton = {
    let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
       button.setTitle("注册", forState: UIControlState.Normal)
    button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "visitorViewRegister", forControlEvents: UIControlEvents.TouchUpInside)
      return button
    }()
    private lazy var loginButton: UIButton = {
    let button = UIButton()
    
    button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        // 点击事件
        button.addTarget(self , action: "visitorViewLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
//    添加遮罩
    private lazy var coverView: UIImageView = {
    let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
    return UIImageView(image: image)
    }()
    
    //设置访客视图信息
    func setupInfo(imageName: String, message: String) {
    iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        messageLabel.numberOfLines = 0
        messageLabel.sizeToFit()
        //隐藏小房子
        homeView.hidden = true
        //遮盖放到最下面
        sendSubviewToBack(coverView)
    }
    
        /// 旋转动画
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
        }
    // MARK: - 核心动画开始和暂停
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
    let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
    // 设置动画速度为0
        iconView.layer.speed = 0
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    /// 恢复旋转
    func resumeAnimation() {
    // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        // 设置动画速度为1
        iconView.layer.speed = 1
        iconView.layer.timeOffset = 0
        iconView.layer.beginTime = 0
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
    
    iconView.layer.beginTime = timeSincePause
    
    }
    // 按钮点击代理
    weak var visitorViewDelegate: HOPVisitorViewDelegate?
    // MARK: - 点击事件
    func visitouViewRegister() {
        visitorViewDelegate?.visitouViewRegister()
       }
    func visitorViewLogin() {
        visitorViewDelegate?.visitorViewLogin()
        
    }
    

    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


